#Replication Code for 
#Digital intermediaries, market competition, and citizen-state interactions
#Authors- Anustubh Agnihotri
#The Files replicates figures and tables that describe
#the distribution of digital intermediaries across jurisdictions 

rm(list = ls())
##Libraries Needed for Replication
pks <- c("tidyverse", "haven", "lfe", "stargazer", 
         "xtable", "kableExtra","ggplot2","dplyr","lubridate","zoo",
         "astsa","panelView")
##Load Libraries
lapply(pks, function(x) if (!require(x, character.only=T)){library(x, character.only = T)})

##Set dir to folder where you have downloaded the files 
dir=''

##Load the datasets
load(paste0(dir,'Intermediaries_Over_Time.RData'))
load(paste0(dir,'Tehsil_HH.RData'))

####Figure 4 in Paper
##Increase in the number of digital intermediaries over time. 

ggplot(Intermediary_Monthwise_Replication,aes(Year_Month,No_Dig_Int))+geom_point()+facet_wrap(~ID)+scale_x_yearmon()+theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+ylab('Number of Digitial Intermediares')


##Aggregate the Data and create a new variable that 
##captures the maximum number of Digital Intermediaries 
##across jurisdictions 

Intermediary_Tehsil_Max<-Intermediary_Monthwise_Replication%>%
  group_by(ID)%>%
  summarise(CSCs_Max=max(No_Dig_Int,na.rm = T))

##Merge the Aggregated Data with the dataset that has number of HHs
Intermediary_Tehsil_Max<-dplyr::left_join(Intermediary_Tehsil_Max,tehsil_rayagada_replication,by='ID')


####Figure 6 in Paper
#Number of VLEs and Population. Figure 6 shows that the expansion of the digital
#intermediary network was linked to the number of potential applicants measured by Number of HHs. The
#number of digital intermediaries in a jurisdiction depended on the number of potential applicants in the
#jurisdiction.
ggplot(Intermediary_Tehsil_Max,aes(TotalHH,CSCs_Max))+geom_point()+
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+
  xlab('Number of Households')+ylab('Number of Digital Intermediaries')+geom_smooth(method='lm',se=FALSE)


